﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class PlcPointInfoRepository:BaseRepository<PlcPointInfo>, IPlcPointInfoRepositpry
    {
        public PlcPointInfoRepository(SqlSugarClient context = null) : base(context)
        {

        }

        public async Task<List<PlcPointInfo>> GetPlcPointInfo(Expression<Func<PlcPointInfo,PlcConnectionInfo,bool>> func)
        {
            var result = await Context.Queryable<PlcPointInfo, PlcConnectionInfo>((l, m) => new JoinQueryInfos
              (JoinType.Inner, l.PlcConnectID == m.ID))
                .Where(func)
                .Select((l, m) => l)
                .ToListAsync();

            return result;
        }

        public async Task<PageDto<PlcPointInfoDto>> GetPlcPointInfoAsync(Expression<Func<PlcPointInfo, PlcConnectionInfo, bool>> exp, PageModel page)
        {
            PageDto<PlcPointInfoDto> pageDto = new PageDto<PlcPointInfoDto>();
            RefAsync<int> totalNumber = 0;
            var result = await Context.Queryable<PlcPointInfo, PlcConnectionInfo>((a, b) => new JoinQueryInfos(
                JoinType.Inner, a.PlcConnectID == b.ID))
                .Where(exp)
                .Select((a, b) => new PlcPointInfoDto
                {
                    ID = a.ID,
                    PlcConnectID = a.PlcConnectID,
                    Floor = a.Floor,
                    ElevatorCode = a.ElevatorCode,
                    CrossroadCode = a.CrossroadCode,
                    ItemName = a.ItemName,
                    ItemValue = a.ItemValue,
                    Address = a.Address,
                    DataType = a.DataType,
                    PlcConnectIP = b.IP
                }).ToPageListAsync(page.PageIndex, page.PageSize, totalNumber);
            pageDto.Result = result;
            pageDto.Total = totalNumber;
            return pageDto;
        }
    }
}
